/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <div class="subject">
        <h3>广告列表</h3>
        <h5>广告索引与管理</h5>
      </div>
    </div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>只需要点击半透明广告图片即可更换广告.</li>
          <li>预览广告所在页面中选择更换你的广告</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>
    <div class="mDiv">
      <div class="ftitle">
        <h3>广告列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
      <div class="searchbox">
        <el-select v-model="ad_position" size="small" clearable placeholder="广告位置 - 所有">
          <el-option
            v-for="item in adPosition"
            :key="item.position_id"
            :label="item.position_name"
            :value="item.position_id"
          ></el-option>
        </el-select>
        <el-input
          class="searchinput"
          v-model="ad_name"
          width="10"
          size="small"
          placeholder="广告名称"
          clearable
        />
        <el-button
          class="searchbtn"
          size="small"
          icon="el-icon-search"
          @click="currentPage = 1;getPage()"
        >搜索</el-button>
      </div>
    </div>
    <div class="tablebox" v-if="tableData">
      <el-table :data="tableData" ref="multipleTable" style="width: 100%;">
        <el-table-column label="广告ID" prop="ad_id" width="100"></el-table-column>
        <el-table-column label="广告位置" prop="ad_position.position_name" width="300"></el-table-column>
        <el-table-column label="广告名称" prop="ad_name" width="200">
          <template slot-scope="scope">{{scope.row.ad_name || '--'}}</template>
        </el-table-column>
        <el-table-column label="广告图片" width="150">
          <template scope="scope">
            <el-image :src="scope.row.ad_code" style="width:80px;height:40px" />
          </template>
        </el-table-column>
        <el-table-column label="新窗口" width="100">
          <template slot-scope="scope">
            <div :class="scope.row.target == 1?'yes':'no'" @click="updateTarget(scope.row)">
              <i :class="scope.row.target == 1?'fa fa-check-circle':'fa fa-ban'"></i>
              <span v-text="scope.row.target == 1?'是':'否'"></span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="显示" width="100">
          <template slot-scope="scope">
            <div :class="scope.row.enabled == 1?'yes':'no'" @click="updateEnable(scope.row)">
              <i :class="scope.row.enabled == 1?'fa fa-check-circle':'fa fa-ban'"></i>
              <span v-text="scope.row.enabled == 1?'是':'否'"></span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="100">
          <template width="10" slot-scope="scope">
            <el-input
              class="sort-input"
              v-model="scope.row.orderby"
              @change="updateAd(scope.row)"
              onkeyup="this.value=this.value.replace(/[^\d.]/g,'');"
              maxlength="3"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                size="small"
                @click="toEdit(scope.row.ad_id)"
                icon="el-icon-edit-outline"
              >编辑</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { getAdPage, updateAd, getADPosition } from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 10,
      adPosition: [],
      ad_position: null,
      ad_name: null
    };
  },
  components: {},
  created() {
    this.getPage();
    this.getADPosition();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
      getAdPage({
        pid: this.ad_position,
        ad_name: this.ad_name,
        page: this.currentPage,
        size: this.pageSize
      }).then(function(res) {
        that.tableData = res.records;
        that.totalCount = res.total;
      });
    },
    refresh() {
      this.getPage();
      this.$message.success("刷新成功！");
    },
    updateTarget(row) {
      var ad = {
        ad_id: row.ad_id,
        target: row.target == 1 ? 0 : 1
      };
      this.updateAd(ad);
    },
    updateEnable(row) {
      var ad = {
        ad_id: row.ad_id,
        enabled: row.enabled == 1 ? 0 : 1
      };
      this.updateAd(ad);
    },
    updateAd(ad) {
      var that = this;
      updateAd(ad).then(function(res) {
        if (res.status == 1) {
          that.getPage();
          that.$message.success("更新成功");
        }
      });
    },
    getADPosition() {
      var that = this;
      getADPosition().then(function(res) {
        that.adPosition = res;
      });
    },
    toEdit(adId) {
      this.$router.push({
        name: "ad_edit",
        params: {
          id: adId,
          currentPage: this.currentPage
        }
      });
    }
  }
};
</script>

<style scoped>
.searchinput {
  width: auto;
}

>>> .sort-input .el-input__inner {
  width: 60px;
}
</style>
